package com.wyyc.common.city.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.wyyc.common.city.repository.CityRepository;
import com.wyyc.common.city.service.CityService;
import com.wyyc.entity.CityDh;
import com.wyyc.entity.CitysEntity;
import com.wyyc.entity.ResJson;
import com.wyyc.ext.ExtInterface;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by Hwei~ on 2017/6/7.
 */
@Service
public class CityServiceImpl implements CityService{

    @Resource
    private CityRepository cityRepository;

    @Override
    public ResJson updCity() {
        ResJson resJson = new ResJson();
        //清空城市数据
        cityRepository.deleteAll();
        //获得最新数据
        String cityJson = ExtInterface.getCitys();
        //System.out.println(cityJson);
        //String cityJson = "{\"BJ\":{\"province\":\"北京\",\"citys\":[{\"city_name\":\"北京\",\"city_code\":\"BJ\",\"abbr\":\"京\",\"engine\":\"1\",\"engineno\":\"0\",\"class\":\"0\",\"classno\":\"0\",\"regist\":\"0\",\"registno\":\"0\"}]},\"SH\":{\"province\":\"上海\",\"citys\":[{\"city_name\":\"上海\",\"city_code\":\"SH\",\"abbr\":\"沪\",\"engine\":\"1\",\"engineno\":\"0\",\"class\":\"0\",\"classno\":\"0\",\"regist\":\"0\",\"registno\":\"0\"}]},\"LN\":{\"province\":\"辽宁\",\"citys\":[{\"city_name\":\"沈阳\",\"city_code\":\"LN_SY\",\"abbr\":\"辽\",\"engine\":\"0\",\"engineno\":\"0\",\"class\":\"1\",\"classno\":\"4\",\"regist\":\"0\",\"registno\":\"0\"},{\"city_name\":\"锦州\",\"city_code\":\"LN_JZ\",\"abbr\":\"辽\",\"engine\":\"0\",\"engineno\":\"0\",\"class\":\"1\",\"classno\":\"6\",\"regist\":\"0\",\"registno\":\"0\"},{\"city_name\":\"营口\",\"city_code\":\"LN_YK\",\"abbr\":\"辽\",\"engine\":\"0\",\"engineno\":\"0\",\"class\":\"1\",\"classno\":\"0\",\"regist\":\"0\",\"registno\":\"0\"},{\"city_name\":\"葫芦岛\",\"city_code\":\"LN_HLD\",\"abbr\":\"辽\",\"engine\":\"0\",\"engineno\":\"0\",\"class\":\"1\",\"classno\":\"0\",\"regist\":\"0\",\"registno\":\"0\"},{\"city_name\":\"大连\",\"city_code\":\"LN_DL\",\"abbr\":\"辽\",\"engine\":\"0\",\"engineno\":\"0\",\"class\":\"1\",\"classno\":\"0\",\"regist\":\"0\",\"registno\":\"0\"},{\"city_name\":\"鞍山\",\"city_code\":\"LN_AS\",\"abbr\":\"辽\",\"engine\":\"0\",\"engineno\":\"0\",\"class\":\"1\",\"classno\":\"4\",\"regist\":\"0\",\"registno\":\"0\"},{\"city_name\":\"抚顺\",\"city_code\":\"LN_FS\",\"abbr\":\"辽\",\"engine\":\"0\",\"engineno\":\"0\",\"class\":\"1\",\"classno\":\"4\",\"regist\":\"0\",\"registno\":\"0\"},{\"city_name\":\"丹东\",\"city_code\":\"LN_DD\",\"abbr\":\"辽\",\"engine\":\"1\",\"engineno\":\"3\",\"class\":\"0\",\"classno\":\"0\",\"regist\":\"0\",\"registno\":\"0\"},{\"city_name\":\"阜新\",\"city_code\":\"LN_FX\",\"abbr\":\"辽\",\"engine\":\"1\",\"engineno\":\"0\",\"class\":\"0\",\"classno\":\"0\",\"regist\":\"0\",\"registno\":\"0\"},{\"city_name\":\"辽阳\",\"city_code\":\"LN_LY\",\"abbr\":\"辽\",\"engine\":\"0\",\"engineno\":\"0\",\"class\":\"1\",\"classno\":\"0\",\"regist\":\"0\",\"registno\":\"0\"},{\"city_name\":\"铁岭\",\"city_code\":\"LN_TL\",\"abbr\":\"辽\",\"engine\":\"0\",\"engineno\":\"0\",\"class\":\"1\",\"classno\":\"0\",\"regist\":\"1\",\"registno\":\"1\"},{\"city_name\":\"朝阳\",\"city_code\":\"LN_CY\",\"abbr\":\"辽\",\"engine\":\"0\",\"engineno\":\"0\",\"class\":\"1\",\"classno\":\"0\",\"regist\":\"0\",\"registno\":\"0\"}]}}";
        Map<String, Object> map = JSON.parseObject(cityJson, new TypeReference<Map<String, Object>>() {});
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            JSONObject jsonObject = JSON.parseObject(entry.getValue().toString());
            String citys = jsonObject.getString("citys");
            List<CityDh> list = JSON.parseArray(citys, CityDh.class);
            String province = jsonObject.getString("province");
            for (CityDh ct:list){
                CitysEntity c = new CitysEntity();
                c.setProvince(province);
                c.setCityCode(ct.getCity_code());
                c.setCityName(ct.getCity_name());
                c.setAbbr(ct.getAbbr());
                c.setEngine(ct.getEngine());
                c.setEngineno(ct.getEngineno());
                c.setClazz(ct.getClassa());
                c.setClassno(ct.getClassno());
                c.setRegist(ct.getRegist());
                c.setRegistno(ct.getRegistno());
                try {
                    cityRepository.save(c);
                }catch (Exception e){
                    resJson.setStatus(1);
                    resJson.setDesc("更新城市信息失败!");
                    return resJson;
                }
            }
        }
        resJson.setStatus(0);
        resJson.setDesc("更新城市信息成功!");
        return resJson;
    }

    @Override
    public ResJson getCityAll() {
        ResJson resJson = new ResJson();
        List<CitysEntity> citysEntityList = cityRepository.findAll();
        resJson.setStatus(0);
        resJson.setDesc("城市信息查询成功!");
        resJson.setResult(JSON.toJSONString(citysEntityList));
        return resJson;
    }
}
